# =====================================================
# PowerShell 自动初始化配置 (项目备份版本)
# 使用方法：将此文件内容复制到您的PowerShell配置文件中
# 配置文件位置：$PROFILE (通常在 Documents\WindowsPowerShell\ 目录下)
# =====================================================

# 设置控制台编码为UTF-8
try {
    # 设置输出编码为UTF-8
    [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
    
    # 设置输入编码为UTF-8
    [Console]::InputEncoding = [System.Text.Encoding]::UTF8
    
    # 设置PowerShell默认编码为UTF-8
    $PSDefaultParameterValues['*:Encoding'] = 'utf8'
    
    # 显示初始化成功信息
    Write-Host "✅ UTF-8编码已自动设置" -ForegroundColor Green
    
} catch {
    Write-Warning "⚠️ UTF-8编码设置失败: $($_.Exception.Message)"
}

# =====================================================
# Docker MySQL 管理函数
# =====================================================

function Start-MySQL {
    Write-Host "启动MySQL容器..." -ForegroundColor Cyan
    docker start quantitative_mysql
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ MySQL容器启动成功" -ForegroundColor Green
        docker ps --filter "name=quantitative_mysql" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
    } else {
        Write-Host "❌ MySQL容器启动失败" -ForegroundColor Red
    }
}

function Stop-MySQL {
    Write-Host "停止MySQL容器..." -ForegroundColor Cyan
    docker stop quantitative_mysql
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ MySQL容器已停止" -ForegroundColor Green
    } else {
        Write-Host "❌ MySQL容器停止失败" -ForegroundColor Red
    }
}

function Show-MySQLStatus {
    Write-Host "MySQL容器状态:" -ForegroundColor Cyan
    $status = docker ps -a --filter "name=quantitative_mysql" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
    if ($status) {
        $status
    } else {
        Write-Host "❌ 未找到MySQL容器" -ForegroundColor Red
    }
}

function Show-MySQLLogs {
    Write-Host "MySQL容器日志 (最近50行):" -ForegroundColor Cyan
    docker logs quantitative_mysql --tail 50
}

function Connect-MySQL {
    Write-Host "连接到MySQL容器..." -ForegroundColor Cyan
    docker exec -it quantitative_mysql mysql -u quantitative_user -pquantitative_password -D quantitative_advanced
}

function Show-MySQLInfo {
    Write-Host ""
    Write-Host "📊 MySQL连接信息:" -ForegroundColor Blue
    Write-Host "   主机: localhost" -ForegroundColor Gray
    Write-Host "   端口: 3306" -ForegroundColor Gray
    Write-Host "   数据库: quantitative_advanced" -ForegroundColor Gray
    Write-Host "   用户名: quantitative_user" -ForegroundColor Gray
    Write-Host "   密码: quantitative_password" -ForegroundColor Gray
    Write-Host ""
}

# 设置别名
Set-Alias mysql-start Start-MySQL
Set-Alias mysql-stop Stop-MySQL
Set-Alias mysql-status Show-MySQLStatus
Set-Alias mysql-logs Show-MySQLLogs
Set-Alias mysql-connect Connect-MySQL
Set-Alias mysql-info Show-MySQLInfo

# =====================================================
# 项目相关快捷命令
# =====================================================

function Go-ProjectRoot {
    Set-Location "G:\QuantitaveZero\QuantitaveAdvan"
    Write-Host "📁 已切换到项目根目录" -ForegroundColor Green
}

function Go-Backend {
    Set-Location "G:\QuantitaveZero\QuantitaveAdvan\backend"
    Write-Host "📁 已切换到后端目录" -ForegroundColor Green
}

function Go-Docker {
    Set-Location "G:\QuantitaveZero\QuantitaveAdvan\backend\docker"
    Write-Host "📁 已切换到Docker目录" -ForegroundColor Green
}

# 设置项目别名
Set-Alias go-root Go-ProjectRoot
Set-Alias go-backend Go-Backend
Set-Alias go-docker Go-Docker

# =====================================================
# 初始化完成提示
# =====================================================

Write-Host ""
Write-Host "🚀 PowerShell初始化完成！" -ForegroundColor Blue
Write-Host ""
Write-Host "📊 MySQL管理命令:" -ForegroundColor Yellow
Write-Host "   mysql-start   - 启动MySQL容器" -ForegroundColor Gray
Write-Host "   mysql-stop    - 停止MySQL容器" -ForegroundColor Gray
Write-Host "   mysql-status  - 查看MySQL状态" -ForegroundColor Gray
Write-Host "   mysql-logs    - 查看MySQL日志" -ForegroundColor Gray
Write-Host "   mysql-connect - 连接到MySQL" -ForegroundColor Gray
Write-Host "   mysql-info    - 显示连接信息" -ForegroundColor Gray
Write-Host ""
Write-Host "📁 项目导航命令:" -ForegroundColor Yellow
Write-Host "   go-root       - 切换到项目根目录" -ForegroundColor Gray
Write-Host "   go-backend    - 切换到后端目录" -ForegroundColor Gray
Write-Host "   go-docker     - 切换到Docker目录" -ForegroundColor Gray
Write-Host ""
